<html>
<head><meta charset="utf-8"><title>cloning `mir::Body` · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html">cloning `mir::Body`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214586014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214586014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214586014">(Oct 26 2020 at 15:10)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/78313">#78313</a> makes it explicit that we clone (somewhat implicitly atm) mir bodies for codegen and inlining.</p>
<p>Considering that we try to avoid cloning the mir body inside of the query system by using <code>Steal</code>, this was surprising to me.</p>
<p>Would it make sense to instead use a <code>BodyWithSubsts { body: &amp;Body&lt;'tcx&gt;, substs: SubstsRef&lt;'tcx&gt; }</code> and substitute on the fly instead of eagerly folding the body?</p>



<a name="214586355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214586355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214586355">(Oct 26 2020 at 15:13)</a>:</h4>
<p>(<em>we clone mir bodies when using <code>body.substs(tcx, substs)</code></em>)</p>



<a name="214586851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214586851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214586851">(Oct 26 2020 at 15:15)</a>:</h4>
<p>That does seem surprising, we're <em>supposed</em> to subst on the fly from what I know</p>



<a name="214587065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214587065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214587065">(Oct 26 2020 at 15:17)</a>:</h4>
<p>let me quickly get the relevant places</p>



<a name="214587112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214587112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214587112">(Oct 26 2020 at 15:17)</a>:</h4>
<p>inlining <a href="https://github.com/rust-lang/rust/blob/9f6c670c4b4273b2c7c0af07a524d4240c926bfc/compiler/rustc_mir/src/transform/inline.rs#L136">https://github.com/rust-lang/rust/blob/9f6c670c4b4273b2c7c0af07a524d4240c926bfc/compiler/rustc_mir/src/transform/inline.rs#L136</a></p>



<a name="214587290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214587290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214587290">(Oct 26 2020 at 15:18)</a>:</h4>
<p>generator drop shims: <a href="https://github.com/rust-lang/rust/blob/9f6c670c4b4273b2c7c0af07a524d4240c926bfc/compiler/rustc_mir/src/shim.rs#L138">https://github.com/rust-lang/rust/blob/9f6c670c4b4273b2c7c0af07a524d4240c926bfc/compiler/rustc_mir/src/shim.rs#L138</a></p>



<a name="214588622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214588622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214588622">(Oct 26 2020 at 15:29)</a>:</h4>
<p>Isn't it expected that inlining is going to clone the inlined body?</p>



<a name="214589126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214589126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214589126">(Oct 26 2020 at 15:32)</a>:</h4>
<p>I think what <span class="user-mention" data-user-id="216206">@lcnr</span> is referring to is that we</p>
<ol>
<li><code>clone</code></li>
<li><code>subst</code></li>
</ol>
<p>when we could be doing that in one step</p>



<a name="214589186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214589186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214589186">(Oct 26 2020 at 15:33)</a>:</h4>
<p>so calling <code>subst</code> for the whole mir bodies has to also clone a lot of <code>Vec</code>s we don't actually want to clone</p>



<a name="214589254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214589254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214589254">(Oct 26 2020 at 15:33)</a>:</h4>
<p>for example <code>required_consts</code> doesn't need to get cloned <a href="https://github.com/rust-lang/rust/blob/9f6c670c4b4273b2c7c0af07a524d4240c926bfc/compiler/rustc_mir/src/transform/inline.rs#L147">https://github.com/rust-lang/rust/blob/9f6c670c4b4273b2c7c0af07a524d4240c926bfc/compiler/rustc_mir/src/transform/inline.rs#L147</a></p>



<a name="214589715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/cloning%20%60mir%3A%3ABody%60/near/214589715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/cloning.20.60mir.3A.3ABody.60.html#214589715">(Oct 26 2020 at 15:36)</a>:</h4>
<p>or in general, rn we do <code>substs[Vec] -&gt; drain -&gt; extend</code> instead of the more optimal <code>iter -&gt; substs[elem] -&gt; extend</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>